package Vista;

import Entidades.Empresa;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author HnosRodriguez
 */
public class EntrevistasPendientes extends javax.swing.JFrame {

    private DefaultListModel modeloLista = new DefaultListModel();
    private ArrayList<Empresa> empresasPendientes = new ArrayList<Empresa>();
    
    /**
     * Creates new form EntrevistasPendientes
     */
    public EntrevistasPendientes() {
        initComponents();
        cargarEntrevistasPendientes();
    }
    
    public EntrevistasPendientes(Empresa e) {
        initComponents();
        cargarAtributosEmpresa(e);
        cargarEntrevistasPendientes();
    }
    
    private void cargarEntrevistasPendientes(){
        FileReader fr = null;
        try {
            fr = new FileReader("entrevistas_pendientes/entrevistas.txt");
            BufferedReader bf = new BufferedReader(fr);
            String cadena;
            try {
                while ((cadena = bf.readLine())!=null) {
                  try {
                      
                    modeloLista.addElement("Llamar a: "
                    +cadena.split(";")[2].split(":")[1]+"; ("
                    +cadena.split(";")[1].split(":")[1].split("/")[0]+":"
                    +cadena.split(";")[1].split(":")[1].split("/")[1]+"); "
                    +cadena.split(";")[0].split(":")[1]+" ("+cadena.split(";")[3].split(":")[1]+")");
                    
                    empresasPendientes.add(new Empresa(cadena.split(";")[0].split(":")[1],
                            cadena.split(";")[3].split(":")[1],cadena.split(";")[1].split(":")[1].split("/")[0]+"/"
                    +cadena.split(";")[1].split(":")[1].split("/")[1],cadena.split(";")[2].split(":")[1]));
                  }
                  catch (ArrayIndexOutOfBoundsException ex){
                      System.out.println("Me fui! con: "+ex.getLocalizedMessage()); 
                  }
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        } catch (FileNotFoundException ex) {
            System.out.println(""+ex.getMessage());
        } finally {
            try {
                fr.close();
                jLEntrevistasPendientes.setModel(modeloLista);
            } catch (IOException ex) {
                System.out.println(""+ex.getMessage());
            }
        }
    }
    
    private void cargarAtributosEmpresa(Empresa e){
        jTNombreEmpresa.setText(e.getNombreEmpresa());
        JTRIFEmpresa.setText(e.getRifEmpresa());
        jTTelfEmpresa.setText(e.getTlfEmpresa());
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jLSponsor = new javax.swing.JLabel();
        jLTitulo = new javax.swing.JLabel();
        jLLogo = new javax.swing.JLabel();
        JTNombrePersonaContacto = new javax.swing.JTextField();
        jLEmpresa = new javax.swing.JLabel();
        jTNombreEmpresa = new javax.swing.JTextField();
        jLentrevistador1 = new javax.swing.JLabel();
        jLEmpresa1 = new javax.swing.JLabel();
        jTTelfEmpresa = new javax.swing.JTextField();
        JTRIFEmpresa = new javax.swing.JTextField();
        jLentrevistador = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLEmpresa2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLEntrevistasPendientes = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jBRealizarEntrevistaPendiente = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jBRegistrarEntrevistaPendiente = new javax.swing.JButton();
        jFHora = new javax.swing.JFormattedTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jBEntrevistasPendientes1 = new javax.swing.JButton();
        jLFondo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLSponsor.setText("Desarrollado para Datinvest C.A");
        jLSponsor.setBounds(110, 40, 220, 14);
        jLayeredPane2.add(jLSponsor, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLTitulo.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLTitulo.setText(".:: Instrumento de emprendedores ::. ");
        jLTitulo.setBounds(90, 20, 270, 14);
        jLayeredPane2.add(jLTitulo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes/DatinvestLogo.png"))); // NOI18N
        jLLogo.setBounds(10, 10, 80, 80);
        jLayeredPane2.add(jLLogo, javax.swing.JLayeredPane.DEFAULT_LAYER);
        JTNombrePersonaContacto.setBounds(380, 220, 140, 20);
        jLayeredPane2.add(JTNombrePersonaContacto, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLEmpresa.setText("Nombre Empresa / Institución: ");
        jLEmpresa.setBounds(220, 100, 160, 14);
        jLayeredPane2.add(jLEmpresa, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTNombreEmpresa.setBounds(380, 100, 140, 20);
        jLayeredPane2.add(jTNombreEmpresa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLentrevistador1.setText("RIF:");
        jLentrevistador1.setBounds(220, 130, 130, 14);
        jLayeredPane2.add(jLentrevistador1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLEmpresa1.setText("Nro. Teléfono:");
        jLEmpresa1.setBounds(220, 160, 120, 14);
        jLayeredPane2.add(jLEmpresa1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTTelfEmpresa.setBounds(380, 160, 140, 20);
        jLayeredPane2.add(jTTelfEmpresa, javax.swing.JLayeredPane.DEFAULT_LAYER);
        JTRIFEmpresa.setBounds(380, 130, 140, 20);
        jLayeredPane2.add(JTRIFEmpresa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLentrevistador.setText("Persona de Contacto:");
        jLentrevistador.setBounds(220, 220, 130, 14);
        jLayeredPane2.add(jLentrevistador, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes/lineaDivisoria.png"))); // NOI18N
        jLabel3.setText("jLabel3");
        jLabel3.setBounds(30, 260, 710, 14);
        jLayeredPane2.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLEmpresa2.setText("Hora nuevo contacto:");
        jLEmpresa2.setBounds(220, 190, 120, 14);
        jLayeredPane2.add(jLEmpresa2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jScrollPane1.setViewportView(jLEntrevistasPendientes);

        jScrollPane1.setBounds(10, 300, 470, 130);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("Y luego haga click en Realizar Entrevista.");
        jLabel1.setBounds(500, 320, 325, 14);
        jLayeredPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Entrevista Pendiente - Registro");
        jLabel2.setBounds(250, 60, 330, 30);
        jLayeredPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jBRealizarEntrevistaPendiente.setText("Realizar Entrevista");
        jBRealizarEntrevistaPendiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRealizarEntrevistaPendienteActionPerformed(evt);
            }
        });
        jBRealizarEntrevistaPendiente.setBounds(570, 350, 170, 23);
        jLayeredPane2.add(jBRealizarEntrevistaPendiente, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Para realizar la entrevista, seleccione de la lista.");
        jLabel4.setBounds(490, 300, 231, 14);
        jLayeredPane2.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Entrevistas Pendientes:");
        jLabel5.setBounds(10, 280, 114, 14);
        jLayeredPane2.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jBRegistrarEntrevistaPendiente.setText("Registrar Entrevista Pendiente");
        jBRegistrarEntrevistaPendiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRegistrarEntrevistaPendienteActionPerformed(evt);
            }
        });
        jBRegistrarEntrevistaPendiente.setBounds(540, 220, 210, 23);
        jLayeredPane2.add(jBRegistrarEntrevistaPendiente, javax.swing.JLayeredPane.DEFAULT_LAYER);

        try {
            jFHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFHora.setBounds(380, 190, 70, 20);
        jLayeredPane2.add(jFHora, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "am", "pm" }));
        jComboBox1.setBounds(460, 190, 39, 20);
        jLayeredPane2.add(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(0, 10, 760, 450);
        jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jBEntrevistasPendientes1.setText("Volver");
        jBEntrevistasPendientes1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEntrevistasPendientes1ActionPerformed(evt);
            }
        });
        jBEntrevistasPendientes1.setBounds(650, 480, 80, 23);
        jLayeredPane1.add(jBEntrevistasPendientes1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLFondo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes/fondoBlanco.png"))); // NOI18N
        jLFondo.setBounds(0, 0, 770, 540);
        jLayeredPane1.add(jLFondo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 765, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
        );

        setSize(new java.awt.Dimension(781, 556));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jBRegistrarEntrevistaPendienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRegistrarEntrevistaPendienteActionPerformed
       
        FileWriter out = null;
        try {
            
            out = new FileWriter("entrevistas_pendientes/entrevistas.txt",true);
            out.write("\n");
            out.write("empresa:"+jTNombreEmpresa.getText()+
                    ";horaContacto:"+jFHora.getText().split(":")[0]+"/"
                    +jFHora.getText().split(":")[1]+""
                    +jComboBox1.getSelectedItem().toString()+";"+
                    "personaContacto:"+JTNombrePersonaContacto.getText()
                    +";rif:"+JTRIFEmpresa.getText());
            
            JOptionPane.showMessageDialog(null,"La entrevista fue agendada "
            + "con éxito","Información",JOptionPane.INFORMATION_MESSAGE);
            
            Presentacion v = new Presentacion();
            v.setVisible(true);
            this.dispose();
            
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }//GEN-LAST:event_jBRegistrarEntrevistaPendienteActionPerformed

    private void jBEntrevistasPendientes1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEntrevistasPendientes1ActionPerformed
            Presentacion v = new Presentacion();
            v.setVisible(true);
            this.dispose();
    }//GEN-LAST:event_jBEntrevistasPendientes1ActionPerformed

    private void jBRealizarEntrevistaPendienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRealizarEntrevistaPendienteActionPerformed
        if (jLEntrevistasPendientes.getSelectedIndex() != -1){
            
            System.out.println("Empresa a eliminar: "
                    +jLEntrevistasPendientes.getSelectedIndex());
            try {
                
                Presentacion p = new Presentacion(empresasPendientes.get(jLEntrevistasPendientes.getSelectedIndex()));
                empresasPendientes.remove(jLEntrevistasPendientes.getSelectedIndex());
                FileWriter w = new FileWriter(new File("entrevistas_pendientes/entrevistas.txt"));
                BufferedWriter bw = new BufferedWriter(w);

                PrintWriter wr = new PrintWriter(bw);  

                for (Empresa e: empresasPendientes){
                    wr.append("empresa:"+e.getNombreEmpresa()+
                        ";horaContacto:"+e.getHoraContacto()+
                        ";personaContacto:"+e.getPersonaContacto()
                        +";rif:"+e.getRifEmpresa());
                }

                wr.close();

                bw.close();
                
                
                this.dispose();
                p.setVisible(true);
                    
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
        else {
             JOptionPane.showMessageDialog(null,"Debe seleccionar un elemento "
            + "del listado","Información",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBRealizarEntrevistaPendienteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EntrevistasPendientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EntrevistasPendientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EntrevistasPendientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EntrevistasPendientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EntrevistasPendientes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTNombrePersonaContacto;
    private javax.swing.JTextField JTRIFEmpresa;
    private javax.swing.JButton jBEntrevistasPendientes1;
    private javax.swing.JButton jBRealizarEntrevistaPendiente;
    private javax.swing.JButton jBRegistrarEntrevistaPendiente;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFormattedTextField jFHora;
    private javax.swing.JLabel jLEmpresa;
    private javax.swing.JLabel jLEmpresa1;
    private javax.swing.JLabel jLEmpresa2;
    private javax.swing.JList jLEntrevistasPendientes;
    private javax.swing.JLabel jLFondo;
    private javax.swing.JLabel jLLogo;
    private javax.swing.JLabel jLSponsor;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLabel jLentrevistador;
    private javax.swing.JLabel jLentrevistador1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTNombreEmpresa;
    private javax.swing.JTextField jTTelfEmpresa;
    // End of variables declaration//GEN-END:variables
}
